//
//  ShakeViewController.m
//  FVQuickMessage
//
//  Created by dllo on 16/8/10.
//  Copyright © 2016年 zty. All rights reserved.
//

#import "ShakeViewController.h"
#import <CoreMotion/CoreMotion.h>
#import "SmileViewController.h"
@interface ShakeViewController ()
@property (nonatomic ,retain) CMMotionManager *motionManager;
@property (nonatomic, strong) NSOperationQueue *quene;
@property (nonatomic, strong) UIImageView *flower;
@end

@implementation ShakeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.motionManager = [[CMMotionManager alloc]init];
    self.quene = [NSOperationQueue mainQueue];
     [self configureAccelerometer];
       // Do any additional setup after loading the view.
}

-(void)viewWillAppear:(BOOL)animated{
   self.view.backgroundColor = [UIColor colorWithRed:52 / 255.0 green:51 / 255.0 blue:62 / 255.0 alpha:1];
    self.navigationItem.title = @"摇一摇";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(leftAction:)];
}
-(void)leftAction:(UIBarButtonItem *)item{
    [self.navigationController popViewControllerAnimated:YES];
}
// 每一个设备晃动的时候, 系统通知每一个在用的设备, 可以使本身成为第一响应者
- (BOOL)canBecomeFirstResponder
{
    return YES;
}
// 加速器的方法
- (void)configureAccelerometer
{
    [_motionManager startAccelerometerUpdates];
    // push 方式
    if ([_motionManager isAccelerometerAvailable]) {
        
        // 设置加速器的频率
        [_motionManager setAccelerometerUpdateInterval:1 / 1000];
        // 开始采集数据
        [_motionManager startAccelerometerUpdatesToQueue:_quene withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
            
            if (fabs(accelerometerData.acceleration.x) > 2.0 || fabs(accelerometerData.acceleration.y) > 2.0 || fabs(accelerometerData.acceleration.z) > 2.0) {
                
                SmileViewController *smileVC = [[SmileViewController alloc] init];
                
                [self.navigationController pushViewController:smileVC animated:YES];
                
            }

            
        }];
    }else{
        NSLog(@"加速器不能使用");
    }
    
    
}

-(void)viewWillDisappear:(BOOL)animated {
    
    [self.motionManager stopAccelerometerUpdates];
    
}

@end
